package com.ljq.rpc.codec;

import com.alibaba.fastjson.JSONObject;
import com.ljq.rpc.common.protocol.dubboProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;
import org.springframework.stereotype.Component;

public class JSONEncoder extends MessageToByteEncoder<dubboProtocol> {

    @Override
    protected void encode(ChannelHandlerContext ctx, dubboProtocol msg, ByteBuf out) throws Exception {
        String json = JSONObject.toJSONString(msg);
        byte[] bytes = json.getBytes(CharsetUtil.UTF_8);
        out.writeInt(bytes.length);
        out.writeBytes(bytes);
    }
}
